package com.example.joemi.carpool;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.baidu.location.Poi;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import java.util.ArrayList;
import java.util.List;

public class SourceActivity extends Activity {

    EditText editText;
    ListView UfWhere;

    //这里复用了terminus的adapter
    TerminusAdapter adapter = null;

    List<PoiInfo> pois = new ArrayList<>();
    PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_source);
        UfWhere = (ListView)findViewById(R.id.poi_list);
        poiSearch = PoiSearch.newInstance();
        OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                if(poiResult == null
                        || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                    Toast.makeText(SourceActivity.this, "未找到结果",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                if(poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    pois = poiResult.getAllPoi();
                    adapter = new TerminusAdapter(SourceActivity.this, pois);
                    UfWhere.setAdapter(adapter);
                }
            }
            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
                Toast.makeText(SourceActivity.this, "PoiDetailResult", Toast.LENGTH_LONG).show();
            }
        };
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
        poiSearch.searchInCity((new PoiCitySearchOption())
                .city("北京")
                .keyword("清华大学"));
        editText = (EditText)findViewById(R.id.edit_text);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                poiSearch.searchInCity((new PoiCitySearchOption())
                        .city("北京")
                        .keyword(editText.getText().toString()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        UfWhere.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainActivity.sourcePoiInfo = pois.get(position);
                setResult(40);
                finish();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_source, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
